// 语言状态管理 slice
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';

// 语言类型定义
export type Language = 'zh' | 'en';

// 语言状态接口
interface LanguageState {
  currentLanguage: Language;
}

// 初始状态 - 默认使用中文
const initialState: LanguageState = {
  currentLanguage: 'zh',
};

// 创建语言 slice
const languageSlice = createSlice({
  name: 'language', // slice 名称
  initialState, // 初始状态
  reducers: {
    // 切换语言动作
    toggleLanguage: (state) => {
      // 在中文和英文之间切换
      state.currentLanguage = state.currentLanguage === 'zh' ? 'en' : 'zh';
    },
    // 设置特定语言动作
    setLanguage: (state, action: PayloadAction<Language>) => {
      state.currentLanguage = action.payload;
    },
  },
});

// 导出动作创建器
export const { toggleLanguage, setLanguage } = languageSlice.actions;

// 导出 reducer
export default languageSlice.reducer;
